----------------------------------------------------------------
-- Copyright (c) 2010-2011 Zipline Games, Inc. 
-- All Rights Reserved. 
-- http://getmoai.com
----------------------------------------------------------------
require "line"
require "player"

function printf ( ... )
	return io.stdout:write ( string.format ( ... ))
end 

MOAISim.openWindow ( "It's Dangerous to Fall Alone", 640, 480 )

viewport = MOAIViewport.new ()
viewport:setSize ( 640, 480 )
viewport:setScale ( 16, 0 )

SCREEN_HEIGHT = 12
SCREEN_WIDTH  = 16

local MAX_SPEED = 6
local MIN_SPEED = 2
local ACCELERATION = 1
local SCORE_PER_SPEED_PER_SECOND = 2.5
local MIN_WIDTH = 5
local MAX_WIDTH = 11
local MAX_SCORE_DIFFICULTY = 2000 -- score at which the difficulty is the hardest. We'll work up to this linearly

local score = 0
local speed = MIN_SPEED

layer = MOAILayer2D.new ()
layer:setViewport ( viewport )
MOAISim.pushRenderPass ( layer )

hudLayer = MOAILayer2D.new()
hudLayer:setViewport(viewport)
MOAISim.pushRenderPass(hudLayer)

function round (num)
	return math.floor(num + 0.5)
end
math.randomseed( os.time() )
function random(min, max)
    return math.random()*(max-min) + min;
end

function onPlayerCollide (event)
    speed = math.max(speed*.75, MIN_SPEED)
end

function getScore()
    return score
end

-- set up the world and start its simulation
world = MOAIBox2DWorld.new ()
world:setGravity ( 0, 0 )
world:setUnitsToMeters ( 2 )
world:setDebugDrawEnabled(false)
layer:setBox2DWorld ( world )

-- initialize the left and right walls ("lines")
local leftLine  = Line(world, -6, SCREEN_HEIGHT/2, SCREEN_HEIGHT*1.5, -MAX_WIDTH/2 - 1, -MAX_WIDTH/2)
local rightLine = Line(world, 6, SCREEN_HEIGHT/2, SCREEN_HEIGHT*1.5, MAX_WIDTH/2, MAX_WIDTH/2 + 1)

leftLine.setSpeed(speed)
rightLine.setSpeed(speed)

-- player
local player = Player(3,2)

world:start ()

-- setup fonts
charcodes = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890.:,;\' "(!?)+-*/='

font = MOAIFont.new ()
font:load('../assets/fonts/AlanDen.ttf')
font:preloadGlyphs(charcodes, 36)

local style = MOAITextStyle.new ()
style:setFont(font)
style:setSize(36)

scoreText = MOAITextBox.new ()
scoreText:setString ( string.format("%d",score) )
scoreText:setStyle(style)
scoreText:setRect(-10,280,90,230)
scoreText:setAlignment ( MOAITextBox.LEFT_JUSTIFY, MOAITextBox.CENTER_JUSTIFY )
scoreText:setYFlip ( true )
scoreText:setScl ( 16/640 )
layer:insertProp ( scoreText )

-----------------
--- GAME LOOP ---
-----------------

elapsed = 0

mainThread = MOAICoroutine.new ()
mainThread:run (
 
 function ()
   local frames = 0
   local lastFrameTime = MOAISim.getDeviceTime()
   while true do
     coroutine.yield ()
     local currentTime = MOAISim.getDeviceTime()
     elapsed = currentTime - lastFrameTime
     lastFrameTime = currentTime

     player.update()
     leftLine.update()
     rightLine.update()

     -- update score
     score = score + SCORE_PER_SPEED_PER_SECOND*speed*elapsed
     scoreText:setString(string.format("%d",score))

     -- increase speed incrementally
     if (speed < MAX_SPEED) then
         speed = speed + (ACCELERATION*elapsed)
         leftLine.setSpeed(speed)
         rightLine.setSpeed(speed)
     end

     -- set width of the wall according to difficulty
     local difficulty = math.min(score, MAX_SCORE_DIFFICULTY)/MAX_SCORE_DIFFICULTY
     local width = MAX_WIDTH - difficulty*(MAX_WIDTH - MIN_WIDTH)

     local jaggedness = random(0.3,1.7)

     leftLine.setBounds(-width/2 - jaggedness, -width/2)
     rightLine.setBounds(width/2, width/2 + jaggedness)
   end
 end
)
